package org.test.client.ui.top;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.image.BufferedImage;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.test.client.ui.MainApplication;
import org.test.client.ui.MainPanel;
import org.test.client.ui.component.btn.DropdownToggleBtn;
import org.test.client.ui.component.btn.TopBlueBtn;
import org.test.client.ui.component.field.HighLightTextField;
import org.test.client.ui.component.field.PlaceHolderTF;
import org.test.client.ui.component.label.MyIconLabel;
import org.test.client.ui.util.FontAwesome;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIConstants;

public class TopPanel extends JPanel{
	
	private MainPanel main = null;
	private TopPanel thisPanel = this;
	
	private JLabel logoLabel = null;
	private JPanel leftContainer = null;
	private JPanel rightContainer = null;
	
	private Color backColor1 = new Color(Integer.parseInt("54b4eb", 16));
	private Color backColor2 = new Color(Integer.parseInt("2fa4e7", 16));
	private Color backColor3 = new Color(Integer.parseInt("1d9ce5", 16));
	
	private TopBlueBtn visitSiteBtn = null;
	private DropdownToggleBtn dropDownBtn = null;
	private DropdownToggleBtn adminBtn = null;
	
	
	private JTextField searchField = null;
	
	public TopPanel(MainPanel main){
		this.main = main;
		doUIInit();
		doLintenerInit();
	}

	public void doUIInit() {
		leftContainer = new JPanel();
		leftContainer.setOpaque(false);
		rightContainer = new JPanel();
		rightContainer.setOpaque(false);
		this.setLayout(new BorderLayout());
		leftContainer.setLayout(new FlowLayout(FlowLayout.LEFT));
		rightContainer.setLayout(new FlowLayout(FlowLayout.RIGHT));
		this.add(leftContainer);
		this.add(rightContainer,BorderLayout.EAST);
		this.setPreferredSize(new Dimension(0, UIConstants.topHeight));
		
		Component placeHolder = Box.createHorizontalStrut(10);
		leftContainer.add(placeHolder);
		MyIconLabel myIconLabel = new MyIconLabel(UIConstants.logoPath,UIConstants.logoTitle,UIConstants.topTitleFont, UIConstants.topHeight - 16);
		leftContainer.add(myIconLabel);
		Component placeHolder2 = Box.createHorizontalStrut(30);
		leftContainer.add(placeHolder2);
		
		BufferedImage icon1 = Icons.getImgByFontHex(FontAwesome.faMap.get("fa-globe"), 19, Color.WHITE);
		visitSiteBtn = new TopBlueBtn("Visit Site", null, 100, UIConstants.topBtnHeigth, icon1);
		leftContainer.add(visitSiteBtn);
		dropDownBtn = new DropdownToggleBtn("fa-star","DropDown",this).doInit();
		leftContainer.add(dropDownBtn);
		Component placeHolder3 = Box.createHorizontalStrut(5);
		leftContainer.add(placeHolder3);
		
		searchField = new PlaceHolderTF("Search");
		searchField.setBackground(Icons.getColor("37a8e8"));
		searchField.setPreferredSize(new Dimension(175,40));
		searchField.setFont(UIConstants.topSearchFont);
		leftContainer.add(searchField);
//		HighLightTextField normalField = HighLightTextField.createRedField();
//		normalField.setFont(UIConstants.placeHolderFont);
//		normalField.setPreferredSize(new Dimension(175,40));
//		normalField.setFont(UIConstants.topSearchFont);
//		leftContainer.add(normalField);
		
		
		adminBtn = new DropdownToggleBtn("fa-user","Admin",thisPanel);
		Color tmpColor = Icons.getColor("555555");
		adminBtn.setIconColor(tmpColor);
		adminBtn.setForeColor(tmpColor);
		adminBtn.setPreWidth(100);
		adminBtn.setUseDefaultUI(true);
		adminBtn.doInit();
		rightContainer.add(adminBtn);
	}

	public void doLintenerInit() {
		
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		//#54b4eb, #2fa4e7 60%, #1d9ce5
		GradientPaint g1 = new GradientPaint(0, 0, backColor1, 0, UIConstants.topHeight/2, backColor2);
		GradientPaint g2 = new GradientPaint(0, UIConstants.topHeight/2, backColor2,  
				0,UIConstants.topHeight, backColor3);
		
		 int width2 = this.getWidth();
		 int height2 = this.getHeight();
		 if(width2 == 0 || height2 ==0){
			 Dimension d = this.getPreferredSize();
			 width2 = d.width; 
			 height2 = d.height; 
//			 System.out.println("top panel 使用 prefersize --------------");
		 }
		 
		 Graphics2D g2d = (Graphics2D)g;
		 
		Paint oldPaint = g2d.getPaint();
		
		g2d.setPaint(g1);
		g2d.fillRect(0, 0, width2, height2/2);
		
		g2d.setPaint(g2);
		g2d.fillRect(0, height2/2, width2, height2/2);
		
	}
	
	public MainApplication getMainFrame(){
		return main.getMainFrame();
	}
}
